package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqWaterProductionEquipmentMaintenanceMonthMapper;
import com.guofen.pojo.SjqCompostProduceMaintenanceMonth;
import com.guofen.pojo.SjqWaterProductionEquipmentMaintenanceMonth;
import com.guofen.service.sjq.SjqWaterProductionEquipmentMaintenanceMonthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SjqWaterProductionEquipmentMaintenanceMonthServiceImpl implements SjqWaterProductionEquipmentMaintenanceMonthService {
    @Autowired
    SjqWaterProductionEquipmentMaintenanceMonthMapper sjqWaterProductionEquipmentMaintenanceMonthMapper;

    @Override
    public boolean add_operator(String equipmentName,String maintenanceContent,String isNormal,String operator,String remarks){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(now);

        SjqWaterProductionEquipmentMaintenanceMonth sjqWaterProductionEquipmentMaintenanceMonth = new SjqWaterProductionEquipmentMaintenanceMonth();
        sjqWaterProductionEquipmentMaintenanceMonth.setMaintenanceTimePlan(date);
        sjqWaterProductionEquipmentMaintenanceMonth.setMaintenanceTimeImplement(date);
        sjqWaterProductionEquipmentMaintenanceMonth.setDate(date);
        sjqWaterProductionEquipmentMaintenanceMonth.setEquipmentName(equipmentName);
        sjqWaterProductionEquipmentMaintenanceMonth.setMaintenanceContent(maintenanceContent);
        sjqWaterProductionEquipmentMaintenanceMonth.setIsNormal(isNormal);
        sjqWaterProductionEquipmentMaintenanceMonth.setRemarks(remarks);
        sjqWaterProductionEquipmentMaintenanceMonth.setOperator(operator);

        int result = sjqWaterProductionEquipmentMaintenanceMonthMapper.insert(sjqWaterProductionEquipmentMaintenanceMonth);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public boolean add_inspector(String id,String inspector,String remarks){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(now);

        SjqWaterProductionEquipmentMaintenanceMonth sjqWaterProductionEquipmentMaintenanceMonth = new SjqWaterProductionEquipmentMaintenanceMonth();
        sjqWaterProductionEquipmentMaintenanceMonth.setId(Integer.parseInt(id));
        //sjqWaterProductionEquipmentMaintenanceMonth.setDate(date);//设置最新还是安旧的来
        sjqWaterProductionEquipmentMaintenanceMonth.setInspector(inspector);
        sjqWaterProductionEquipmentMaintenanceMonth.setRemarks(remarks);

        int result = sjqWaterProductionEquipmentMaintenanceMonthMapper.updateByPrimaryKeySelective(sjqWaterProductionEquipmentMaintenanceMonth);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public SjqWaterProductionEquipmentMaintenanceMonth getlast(String equipmentName,String maintenanceContent){
        Map<String, Object> data = new HashMap<>();
        data.put("equipmentName", equipmentName);
        data.put("maintenanceContent", maintenanceContent);
        return sjqWaterProductionEquipmentMaintenanceMonthMapper.getlast(data);
    }

    @Override
    public List<SjqWaterProductionEquipmentMaintenanceMonth> getlastByName(String equipmentName){
        return sjqWaterProductionEquipmentMaintenanceMonthMapper.getlastByName(equipmentName);
    }

    @Override
    public List<Integer> is_last_complete(String equipmentsName, String menstanceNum, String role){
        List<Integer> res = new ArrayList<Integer>();
        String[] equipments = equipmentsName.split("_");
        String[] num = menstanceNum.split("_");
        boolean breakkey = false;

        for(int i=0;i<equipments.length ;i++){
            breakkey = false;
            List<SjqWaterProductionEquipmentMaintenanceMonth> items = sjqWaterProductionEquipmentMaintenanceMonthMapper.getlastByName(equipments[i]);
            if (items.size() == Integer.parseInt(num[i])){
                if(role.equals("examiner")){
                    for(int j=0;j<items.size();j++){
                        if(items.get(j).getInspector()==null||items.get(j).getInspector().equals("")){
                            res.add(1);
                            breakkey = true;
                            break;
                        }
                    }
                }
                if(breakkey)continue;
                res.add(2);
            }else{
                res.add(0);
            }
        }
        return res;
    }

    @Override
    public int getInThisMonth() {
        return sjqWaterProductionEquipmentMaintenanceMonthMapper.selectInThisMonth();
    }

    @Override
    public List<SjqWaterProductionEquipmentMaintenanceMonth> getMonthRecords(String equipmentName) {
        return sjqWaterProductionEquipmentMaintenanceMonthMapper.getMonthRecords(equipmentName);
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqWaterProductionEquipmentMaintenanceMonth> list = sjqWaterProductionEquipmentMaintenanceMonthMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqWaterProductionEquipmentMaintenanceMonth> list = sjqWaterProductionEquipmentMaintenanceMonthMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqWaterProductionEquipmentMaintenanceMonthMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqWaterProductionEquipmentMaintenanceMonth record) {
        return sjqWaterProductionEquipmentMaintenanceMonthMapper.updateByPrimaryKeySelective(record);
    }
}
